package com.example.zx.theline;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.lyjh.lyyjh.JhshActivity;
import com.example.zx.theline.sgjh.SgjhActivity;
import com.example.zx.theline.lyjh.lyrjh.SgrjhActivity;
import com.example.zx.theline.lyjh.lyzjh.SgzjhActivity;
import com.example.zx.theline.usercenter.UserCenterActivity;
import com.example.zx.theline.zsjh.ZsjhActivity;

import Utils.ActivityManager;
import Utils.Constant;
import Utils.RunningInfoUtil;
import Utils.ShareUtil;
import app.MyApplication;
import base.BaseActivity;
import dialog.CenterDialog;
import dialog.FirstInstallDialog;
import greendao.UserDao;

import me.xiaopan.android.widget.LinearLineWrapLayout;
import views.MenuItem;
import user.LoginUser;

public class BodyActivity extends BaseActivity {
    private TextView title, tcdl;
    private TextView username, userunite;
    private UserDao mUserdao;
    private LoginUser mLoginUser;
    //初始化提示dialog
    private FirstInstallDialog mFirstInstallDialog;
    private CenterDialog mCenterDialog;
    private LinearLineWrapLayout menu;

    private long mExitTime = 0;
    private static final int EXIT_TIME = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body);
        InitView();
        InitData();
        InitMenu();
        InitListen();
    }

    @Override
    protected void InitView() {
        mLoginUser = MyApplication.getmLoginUser();
        title = (TextView) findViewById(R.id.title);
        menu = (LinearLineWrapLayout) findViewById(R.id.menu);
        tcdl = (TextView) findViewById(R.id.tcdl);
        username = (TextView) findViewById(R.id.username);
        userunite = (TextView) findViewById(R.id.userunite);
    }

    @Override
    protected void InitData() {
        title.setText("菜单");
        mUserdao = MyApplication.getDaoSession().getUserDao();
        username.setText(MyApplication.getmLoginUser().getTrueName());
        userunite.setText(MyApplication.getmLoginUser().getUserUnitName());
        //获取app使用相关的用户数据单位数据
        if (isFirst()){
            IniteAppInfo();
        }
    }

    private void InitMenu() {
        //邻营月计划
        if (MyApplication.getmLoginUser().getSgyjh().isSgyjh()) {
            MenuItem sgyjh = new MenuItem(this, R.drawable.plan_yue, "邻营月计划");
            menu.addView(sgyjh);
            sgyjh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(BodyActivity.this, JhshActivity.class));
                }
            });
        }
        //邻营周计划
        if (MyApplication.getmLoginUser().getSgzjh().isSgzjh()) {
            MenuItem sgzjh = new MenuItem(this, R.drawable.plan_zhou, "邻营周计划");
            menu.addView(sgzjh);
            sgzjh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(BodyActivity.this, SgzjhActivity.class));
                }
            });
        }
        //邻营日计划
        if (MyApplication.getmLoginUser().getSgrjh().isSgrjh()) {
            MenuItem sgrjh = new MenuItem(this, R.drawable.plan_ri, "邻营日计划");
            menu.addView(sgrjh);
            sgrjh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(BodyActivity.this, SgrjhActivity.class));
                }
            });
        }
        //施工监护
        if (MyApplication.getmLoginUser().getJdjc().isSgjh()) {
            MenuItem sgjh = new MenuItem(this, R.drawable.check, "施工监护");
            menu.addView(sgjh);
            sgjh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(BodyActivity.this, SgjhActivity.class));
                }
            });
        }
        //监督检查计划
        if (MyApplication.getmLoginUser().getJdjc().isJclr()) {
            MenuItem jclr = new MenuItem(this, R.drawable.write, "监督检查计划");
            menu.addView(jclr);
            jclr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(BodyActivity.this, JclrActivity.class));
                }
            });
        }
        //正式计划
        if (MyApplication.getmLoginUser().getZsjh().isZsjh()) {
            MenuItem zsjh = new MenuItem(this, R.drawable.fplan, "正式计划");
            menu.addView(zsjh);
            zsjh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(BodyActivity.this, ZsjhActivity.class));
                }
            });
        }
        //用户中心
        MenuItem yhzx = new MenuItem(this, R.drawable.usercenter, "用户中心");
        menu.addView(yhzx);
        yhzx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BodyActivity.this, UserCenterActivity.class));
            }
        });
    }

    @Override
    protected void InitListen() {
        tcdl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                breakLogin();
            }
        });
    }

    //切换账号
    public void breakLogin() {
        mCenterDialog = new CenterDialog(BodyActivity.this, new CenterDialog.CenterClick() {
            @Override
            public void confirmListener() {
                mCenterDialog.show();
                finish();
                //清除登录记录
                MyApplication.getmLoginUser().logOut();
                //清楚浏览记录
                RunningInfoUtil.clear();
            }

            @Override
            public void cancelListener() {
                mCenterDialog.dismiss();
            }
        });
        mCenterDialog.setMessage("您确定要退出账号？");
        mCenterDialog.show();
    }

    //初始化app用户字典数据数据
    private void IniteAppInfo() {
        if (isLoadInfo()) {
            mFirstInstallDialog = new FirstInstallDialog(this);
            mFirstInstallDialog.show();
        }
    }

    //根据本地是否有登录单位信息 返回值 boolean 有：true 没有：false
    public boolean isLoadInfo() {
        if (MyApplication.getDaoSession().getUserDao().queryBuilder().
                where(UserDao.Properties.LjyyxDwxxId.eq(MyApplication.getmLoginUser().getUserUnitId()))
                .list()
                .size() == 0) {
            return true;
        }
        return false;
    }

    //判断程序是否第一次运行
    private boolean isFirst() {
        boolean isFirst = ShareUtil.getBoolean(this, Constant.SHARE_IS_FIRST, true);
        if (isFirst) {
            //标记我们已经启动过App了
            ShareUtil.putBoolean(this, Constant.SHARE_IS_FIRST, false);
            //是第一次运行
            return true;
        } else {
            return false;
        }
    }

    //按两次退出应用程序
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - mExitTime) > EXIT_TIME) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                // 退出代码
                ActivityManager.getInstance().AppExit(this);
                if (mExitTime != 0) {
                    mExitTime = 0;
                }
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public static void startThis(Context context) {
        Intent mIntent = new Intent(context, BodyActivity.class);
        context.startActivity(mIntent);
        if (context instanceof Activity) {
            Activity mActivity = (Activity) context;
            mActivity.finish();
        }
    }

}